import 'package:inmotion_pro/config/const.dart';
import 'package:inmotion_pro/routes/app_routes.dart';
import 'package:inmotion_pro/service/global_service.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

// 认证登录中间件
class AuthLoginMiddleware extends GetMiddleware {
  @override
  int? get priority => -1;

  @override
  RouteSettings? redirect(String? route) {
    // 可以不需要登录的路由列表
    List<String> filterRoutes = [AppRoutes.ROOT];
    // 检查用户是否登录
    if (GlobalService.to.isLoggedIn ||
        (filterRoutes.contains(route) &&
            GlobalService.to.cacheUserInfo.value.uid == guestUid)) {
      // 继续导航到目标页面
      return null;
    } else {
      // 未登录，重定向到登录页
      return const RouteSettings(name: AppRoutes.LOGIN_REGISTER);
    }
  }

  @override
  GetPage? onPageCalled(GetPage? page) {
    // 页面被调用时执行其他逻辑
    return page;
  }
}
